<template>
  <div class="app-container">
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="auto"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depotCode')" prop="depotCode">
            <el-input
              v-model="queryParams.depotCode"
              :placeholder="$t('pleaseEnter')"
              clearable
              filterable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depotName')" prop="depotName">
            <el-input
              v-model="queryParams.depotName"
              :placeholder="$t('pleaseEnter')"
              clearable
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depotStatus')" prop="depotStatus">
            <el-select
              v-model="queryParams.depotStatus"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotStatusOptions"
                :key="item.value"
                :value="item.value"
                :label="$t(item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depotType')" prop="depotType">
            <el-select
              v-model="queryParams.depotType"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotTypeOptions"
                :key="item.value"
                :value="item.value"
                :label="$t('depotLang.' + item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
        >
          {{ $t("add") }}</el-button>
      </el-col>
    </el-row>
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="100"
      />
      <el-table-column
        :label="$t('depotLang.depotCodeAndName')"
        prop="depotCodeAndName"
        align="center"
        min-width="150"
      />
      <el-table-column
        :label="$t('depotLang.depotStatus')"
        align="center"
        min-width="100"
      >
        <template v-slot="{ row }">
          <el-tag :type="depotStatusTag[row.depotStatus]">
            {{ $t(getTitleByValue(depotStatusOptions, row.depotStatus)) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('depotLang.depotType')"
        prop="depotType"
        align="center"
        min-width="100"
      >
        <template v-slot="{ row }">
          <div>
            {{
              $t(
                "depotLang." + getTitleByValue(depotTypeOptions, row.depotType)
              )
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('depotLang.timeZone')"
        prop="timeZone"
        align="center"
        min-width="100"
      />
      <el-table-column
        :label="$t('depotLang.localDateTime')"
        prop="localDateTime"
        align="center"
        min-width="100"
      />
      <el-table-column
        :label="$t('depotLang.country')"
        prop="countryName"
        align="center"
        min-width="100"
      />
      <el-table-column
        :label="$t('depotLang.province')"
        prop="province"
        align="center"
        min-width="160"
      />

      <el-table-column
        :label="$t('operation')"
        width="220"
        align="center"
        fixed="right"
      >
        <template v-slot="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ $t("edit") }}
          </el-button>

          <el-button
            v-if="row.depotStatus !== depotStatusDict.ENABLE_STATUS.value"
            type="text"
            @click="handleEnable(row)"
          >
            {{ $t("enable") }}
          </el-button>
          <el-button
            v-if="row.depotStatus === depotStatusDict.ENABLE_STATUS.value"
            type="text"
            @click="handleDisable(row)"
          >
            {{ $t("disable") }}
          </el-button>
          <el-button
            v-if="row.depotStatus === depotStatusDict.DRAFT_STATUS.value"
            type="text"
            @click="handleDelete(row)"
          >
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 新增/编辑弹层 -->
    <DepotBaseForm
      v-model="depotBaseParams.id"
      :visible.sync="depotDialog"
      :title="title"
      :operation="depotBaseParams.operation"
      @close="close"
    />
  </div>
</template>

<script>
import DepotBaseForm from './components/info-form.vue'
import { depotTypeDict, depotStatusDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import {
  getDepotPageList,
  deleteDepot,
  setDepotStatusEnable,
  setDepotStatusDisable
} from '@/api/business/depotManage/depot'

export default {
  name: 'Depot',
  components: { DepotBaseForm },
  data() {
    return {
      total: 0,
      loading: false,
      queryParams: {
        depotCode: null,
        depotName: null,
        depotType: null,
        depotStatus: null,
        pageNo: 1,
        pageSize: 10
      },
      tableData: [], // 仓库列表
      depotStatusDict: depotStatusDict,
      getTitleByValue: getTitleByValue,
      depotStatusTag: ['info', 'success', 'danger'],
      depotStatusOptions: getDictOptionList(depotStatusDict), // 仓库状态
      depotTypeOptions: getDictOptionList(depotTypeDict), // 仓库类型
      depotBaseParams: {
        id: null,
        operation: 0
      },
      depotDialog: false,
      title: ''
    }
  },
  created() {
    this.getDepotPageList()
  },
  methods: {
    // 重置
    async handleReset() {
      await this.$refs.queryParams.resetFields()
      this.getDepotPageList()
    },
    handleOpenInfo(row) {
      this.depotBaseParams.operation = 0
      this.depotBaseParams.id = row.depotId
      this.title = "$t('info')"
      setTimeout(() => {
        this.depotDialog = true
      }, 400)
    },
    // 查询
    async handleQuery() {
      this.loading = true
      this.queryParams.pageNo = 1
      this.getDepotPageList()
      this.loading = false
    },
    // 获取仓库列表
    async getDepotPageList() {
      const res = await getDepotPageList({ ...this.queryParams })
      // console.log(res);
      this.tableData = res.data.resultList
      this.total = res.data.totalRowNum
    },
    // 新增
    async handleAdd() {
      this.depotBaseParams.operation = 1
      this.title = this.$t('add')
      this.depotDialog = true
    },
    async handleSubmit() {
      this.$refs.queryParams.submit()
    },
    // 编辑
    async handleEdit(row) {
      this.depotBaseParams.operation = 2
      this.depotBaseParams.id = row.depotId
      this.title = this.$t('edit')
      setTimeout(() => {
        this.depotDialog = true
      }, 400)
    },
    // 删除
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t('delete') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      // console.log(flag);
      if (flag !== 'cancel') {
        const res = await deleteDepot({
          depotId: row.depotId,
          modifiedUser: 0
        })
        if (!res) return
        this.$message({
          message: this.$t('handleSuccess'),
          type: 'success'
        })
        this.getDepotPageList()
      }
    },
    // 启用
    async handleEnable(row) {
      const flag = await this.$confirm(
        this.$t('enable') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotStatusEnable({
        depotId: row.depotId,
        modifiedUser: 0
      })
      if (!res || res.returnCode !== 10000) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getDepotPageList()
    },
    // 禁用
    async handleDisable(row) {
      const flag = await this.$confirm(
        this.$t('disable') + '?',
        this.$t('hint'),
        {
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await setDepotStatusDisable({
        depotId: row.depotId,
        modifiedUser: 0
      })
      if (!res || res.returnCode !== 10000) return
      this.$message({
        message: this.$t('handleSuccess'),
        type: 'success'
      })
      this.getDepotPageList()
    },
    // 关闭弹出层
    close() {
      this.depotBaseParams.id = null
      this.depotBaseParams.operation = 0
      this.depotDialog = false
      this.getDepotPageList()
    },
    // 切换页码
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getDepotPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    }
  }
}
</script>

<style scss="lang" scoped></style>
